﻿Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
                o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
Array.prototype._indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i].id == val.id) return i;
    }
    return -1;
};
Array.prototype._getValue = function (id) {
    for (var i = 0; i < this.length; i++) {
        if (this[i].id == id) return this[i];
    }
    return -1;
};
Array.prototype.remove = function (val) {
    var index = this._indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};
util = function () { };
util.toDate = function(val) {
    val = val.replace(/-/g, "/");
    return new Date(val);
};
util.formateDate = function (date, formate) {
    //if (formate == "yyyy-MM-dd")
    //    return date.substring(0, date.indexOf(' '));
    //if (formate == "yyyy-MM-dd hh:mm")
    //    return date.substring(0, date.lastIndexOf(':'));
    //return date;
    if (!date) return "";
    if (date.indexOf('-') > -1 && date.indexOf('(-') == -1) return date;
    var milliseconds = parseInt(date.replace("/Date(", "").replace(")/", ""));
    if (!formate)
        return new Date(milliseconds).format("yyyy-MM-dd hh:mm");
    else {
        return new Date(milliseconds).format(formate);
    }
}
util.formateTitle = function (title, num, omission) {
    if (typeof (title) == "number") return title;
    num = num * 2;
    if (!title || title == "[object Object]" || title == "") {
        return "";
    }
    var ret = 0;
    var retStr = "";
    var ch;
    var charCode;
    var flag = false;
    for (var i = 0; i < title.length; i++) {
        ch = title.charAt(i);
        charCode = ch.charCodeAt(0);
        if (ret == num || ((ret == num - 1) && (charCode < 0 || charCode > 255))) {
            flag = true;
            break;
        }
        if (charCode < 0 || charCode > 255 || (charCode > 64 && charCode < 91)) {
            ret += 2;
        } else {
            ret += 1;
        }
        retStr += ch;
    }
    if (omission && flag) {
        retStr = retStr.substr(0, retStr.length - 1) + "...";
    }
    return retStr;
};
util.getUrlParam = function (name) {
    var sValue = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]*)(\&?)", "i"));
    return sValue ? sValue[1] : sValue;
};

util.updateUrlParams = function (url, name, value) {
    var r = url;
    if (r != null && r != 'undefined' && r != "") {
        value = encodeURIComponent(value);
        var reg = new RegExp("(^|)" + name + "=([^&]*)(|$)");
        var tmp = name + "=" + value;
        if (url.match(reg) != null) {
            r = url.replace(eval(reg), tmp);
        } else {
            if (url.match("[\?]")) {
                r = url + "&" + tmp;
            } else {
                r = url + "?" + tmp;
            }
        }
    }
    return r;
};
util.getJSON = function (url, paras, func) {
    url = util.updateUrlParams(url, "time", (new Date()).getTime());
    $.getJSON(url, paras, func);
};
util.post = function (url, paras, func) {
    $.post(url, paras, function (json) {
        func(json);
    });
};
util.postJson = function (url, data, func) {
    $.ajax({
        url: url,
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(data),
        contentType: 'application/json; charset=utf-8',
        success: function(ret) {
            func(ret);
        }
    });
};
util.getNowDate = function() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = util.FormateZero(date.getHours(),2);
    var minute = util.FormateZero(date.getMinutes(),2);
    return year + "-" + month + "-" + day + " " + hour + ":" + minute;
}
util.FormateZero=function(val,num) {
    var i = num - val.length;
    for (var m = 0; m < i; m++) {
        val = "0" + val;
    }
    return val;
}
util.FormateNull = function (val) {
    if(!val || val=='null')
        return "";
    return val;
}
function getMousePoint(ev) {
    //定义鼠标在视窗中的位置 
    var point = {
        x: 0,
        y: 0
    };
    //如果浏览器支持 pageYOffset, 通过 pageXOffset 和 pageYOffset 获取页面和视窗之间的距离 
    if (typeof window.pageYOffset != 'undefined') {
        point.x = window.pageXOffset;
        point.y = window.pageYOffset;
    }
        //如果浏览器支持 compatMode, 并且指定了 DOCTYPE, 通过 documentElement 获取滚动距离作为页面和视窗间的距离 
        //IE 中, 当页面指定 DOCTYPE, compatMode 的值是 CSS1Compat, 否则 compatMode 的值是 BackCompat 
    else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
        point.x = document.documentElement.scrollLeft;
        point.y = document.documentElement.scrollTop;
    }
        //如果浏览器支持 document.body, 可以通过 document.body 来获取滚动高度 
    else if (typeof document.body != 'undefined') {
        point.x = document.body.scrollLeft;
        point.y = document.body.scrollTop;
    }
    //加上鼠标在视窗中的位置 
    point.x += ev.clientX;
    point.y += ev.clientY;
    //返回鼠标在视窗中的位置 
    return point;
}
function showContent(title, content) {
    var browser = arguments.callee.caller.arguments[0] || window.event;
    var point = getMousePoint(browser);
    $("#divContent").show();
    var left = point.x;
    if (document.body.offsetWidth - left < 180) {
        left = document.body.offsetWidth - 180;
    }
    $("#divContent").css("left", left);
    $("#divContent").css("top", point.y);
    var html = "标题：" + title + "</br>";
    html += "内容：" + content;
    $("#divContent").html(html);
}

function hideContent() {
    $("#divContent").hide();
}